import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

Vue.use(ElementUI)

Vue.config.productionTip = false

import axios from 'axios'          //引入axios


Vue.prototype.$axios = axios;      //把axios挂载到vue上
axios.defaults.withCredentials = true
import Axios from 'axios'

axios.interceptors.response.use(res => {
  return res;
}, error => {
  console.log(error.response.status)
  if (error.response.status) {
    switch (error.response.status) {
      case 401:
        window.localStorage.clear()
        router.push('/user')
        break;
      case 402:
        window.localStorage.clear()
        location.reload();
        break;
    }
  }
  return Promise.reject(new Error(error));
})

import Cookies from 'js-cookie'

// 处理csrf问题
let getCookie = function (cookie) {
  let reg = /csrftoken=([\w]+)[;]?/g
  return reg.exec(cookie)[1]

}

Axios.interceptors.request.use(
function(config) {
  // 在post请求前统一添加X-CSRFToken的header信息
  let cookie = document.cookie;
  if(cookie && config.method == 'post'){
    config.headers['X-CSRFToken'] = Cookies.get('csrftoken');
  }
  return config;
},
function(error) {
  // Do something with request error
  return Promise.reject(error);
});

// Vue.http.options.xhr = { withCredentials: true };
// Vue.http.options.emulateJSON = true;

Vue.prototype.BACK_HOSTNAME="http://152.136.26.199"
Vue.prototype.BACK_URL="http://152.136.26.199:8000/"
// axios.defaults.baseURL = "http://152.136.26.199:8000/"
axios.defaults.baseURL = "http://localhost:8081/"

import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
Vue.use(mavonEditor)


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
